package com.wwwarehouse.financialcenter.fragment.moneywarehouse.bills;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.bean.bill.BillOrderGoodsResponseBean;
import com.wwwarehouse.financialcenter.bean.bill.SerializableMap;
import com.wwwarehouse.financialcenter.constant.FinancialConstant;
import com.wwwarehouse.financialcenter.fragment.base.TheParentFragment;

import java.util.List;
import java.util.Map;

/**
 * @author Joseph on 2017/11/15.
 *         <p/>
 *         订购商品子类列表界面
 */

public class BillOrderGoodsChildFragment extends TheParentFragment {
    private ListView mListView;
    private InformationListAdapter mAdapter;

    private List<BillOrderGoodsResponseBean.BillOrderGoodsDetailsBean> mBillOrderGoodsDetailsList;

    private static final int REQUEST_GET_ORDER_DETAILS = 0;

    @Override
    protected int getContentId() {
        return R.layout.financial_center_fragment_list;
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    protected void init(View view) {
        mListView = findView(view, R.id.lv_list);
    }

    @Override
    protected void loadDatas() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            SerializableMap serializableMap = (SerializableMap) bundle.getSerializable("map");
            Map map = serializableMap.getMap();

            int pos = bundle.getInt("pos");
            int num = bundle.getInt("num");

            map.put("page", pos);
            map.put("size", num);

            httpPost(FinancialConstant.URL_LIST_ORDER_DETAILS, map, REQUEST_GET_ORDER_DETAILS, true, null);
        }
    }

    @Override
    public void setTitle() {

    }

    private class InformationListAdapter extends BaseAdapter {

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            BillOrderGoodsResponseBean.BillOrderGoodsDetailsBean billOrderGoodsDetailsBean = mBillOrderGoodsDetailsList.get(position);

            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.financial_center_adapter_order_goods, parent, false);

                ViewTreeObserver viewTreeObserver = mListView.getViewTreeObserver();
                final View finalConvertView = convertView;

                viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        mListView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                        ViewGroup.LayoutParams layoutParams = finalConvertView.getLayoutParams();
                        int itemHeight = mListView.getHeight() / 7;
                        layoutParams.height = itemHeight;

                        finalConvertView.setLayoutParams(layoutParams);
                    }
                });

                holder.icon = (ImageView) convertView.findViewById(R.id.img);
                holder.name = (TextView) convertView.findViewById(R.id.goods_name);
                holder.price = (TextView) convertView.findViewById(R.id.goods_price);
                holder.count = (TextView) convertView.findViewById(R.id.goods_count);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            if (!TextUtils.isEmpty(billOrderGoodsDetailsBean.getImg())) {
                BaseApplication.getApplicationInstance().displayImg(billOrderGoodsDetailsBean.getImg(), holder.icon);
            }

            holder.name.setText("￥" + billOrderGoodsDetailsBean.getRsName());
            holder.price.setText(billOrderGoodsDetailsBean.getOriginPrice());
            holder.count.setText("x" + billOrderGoodsDetailsBean.getRsQty() + billOrderGoodsDetailsBean.getRsUnit());

            return convertView;
        }

        @Override
        public int getCount() {
            return mBillOrderGoodsDetailsList.size();
        }

        @Override
        public Object getItem(int position) {
            return mBillOrderGoodsDetailsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
    }

    class ViewHolder {
        ImageView icon;
        TextView name;
        TextView price;
        TextView count;
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what){
            case REQUEST_GET_ORDER_DETAILS:
                if ("0".equals(commonClass.getCode())) {
                    BillOrderGoodsResponseBean billOrderGoodsResponseBean = JSON.parseObject(commonClass.getData().toString(), BillOrderGoodsResponseBean.class);
                    mBillOrderGoodsDetailsList = billOrderGoodsResponseBean.getList();

                    mAdapter = new InformationListAdapter();
                    mListView.setAdapter(mAdapter);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onReLoad(int code) {

    }
}
